package cn.powertime.evaluation.config;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public  class AlUtils {

    private static String ACCESSKEYID = "LTAI4FqC7ZFZYZpF2r3uKNfY";
    private static String SECRET = "8PEkPMsNyBsZZ9AvVvExVM7u3nfr6f";


    public static boolean send(String phoneNum,String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESSKEYID, SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        code = "{\"code\":\""+code+"\"}";
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "时代新威");
        request.putQueryParameter("TemplateCode", "SMS_178985512");
        request.putQueryParameter("TemplateParam", code);

        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String s =response.getData();
            JSONObject  jsonObject =  JSONObject.parseObject(s);
            String c =  jsonObject.getString("Message");
            if("OK".equals(c)){
                return true;
            }else{
                return false;
            }
        } catch (ServerException e) {
            e.printStackTrace();
            return false;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }finally {
        }
    }
}